package com.liuqing.www.phone.server.底层消息;

import java.io.IOException;
import java.net.SocketTimeoutException;
import java.nio.channels.ClosedChannelException;
import java.util.ArrayList;
import java.util.List;

import javax.sound.midi.SysexMessage;

import org.apache.log4j.Logger;
import org.xsocket.connection.INonBlockingConnection;

import com.gwsoft.logic.NetCommand;
import com.gwsoft.logic.common.CommandModel;
import com.gwsoft.logic.user.OnlineUser;
import com.gwsoft.server.SessionManager;

/**
 * @author kevinbian E-mail: langzibian@yahoo.com.cn
 * @version 创建时间：2011-5-4 上午11:00:38
 * 
 */
public class DataBatchSend implements Runnable{
	
	private static Logger logger = Logger.getLogger(DataBatchSend.class) ;
	
//	private byte[] sendData = null ;
	
	private int reqCmd = 0 ;
	
	private int cmdIndex = 0 ;
	
	private byte[] dataPack = null ;
	
	private List<OnlineUser> sendUserList = new ArrayList<OnlineUser>() ;
	
	private List<byte[]> batchData = new ArrayList<byte[]>();
	/**
	 * 
	 * @param reqCmd 触发命令
	 * @param cmdIndex	批量发送索引
	 * @param dataData	需要发送数据(不包括头)
	 * @param list	发送给的玩家
	 */
	public DataBatchSend(int reqCmd ,int cmdIndex ,List<byte[]> batchData ,List<OnlineUser> list) {
		this.reqCmd = reqCmd ;
		this.cmdIndex = cmdIndex ;
//		this.sendData = sendData ;
		this.batchData = batchData ;
		this.sendUserList = list ;
		Thread thread = new Thread(this);
		thread.start() ;
	}
	
	public void sendBatch() throws Exception{
		
		CommandModel cmdModel = NetCommand.getCommand(reqCmd) ;
		
		int res_cmd_other = cmdModel.getRes_cmd_other().get(cmdIndex) ;
		System.out.println("批量发送命令 :"+Integer.toHexString(res_cmd_other));
		
		if(cmdModel == null && cmdModel.getRes_cmd_other().size() == 0){
			logger.error("批量发送命令未找到:"+Integer.toHexString(reqCmd)) ;
			System.err.println("批量发送命令未找到:"+Integer.toHexString(reqCmd));
			return ;
//			throw new Exception("批量发送命令未找到:"+Integer.toHexString(reqCmd)) ;
		}
//		System.out.println("批量命令"+Integer.toHexString(cmdModel.getRes_cmd_other().get(cmdIndex)));
		
		if(sendUserList.size() == 0){
			logger.error("批量发送无有效用户") ;
			System.err.println("批量发送无有效用户");
			return ;
//			throw new Exception("批量发送无有效用户") ;
		}
		if(batchData.size() == 0){
			logger.error("批量发送命令无数据") ;
			System.err.println("批量发送命令无数据");
			return ;
//			throw new Exception("批量发送命令无数据") ;
		}
		
		if(sendUserList.size() > 0){
			System.out.println("该发送的玩家数量 "+sendUserList.size());
			int dataIndex = 0 ;
			for(OnlineUser user : sendUserList){
				
				byte[] sendData = null ;
				
				if(batchData.size() != sendUserList.size()){
					sendData = batchData.get(0) ;
				}else{
					sendData = batchData.get(dataIndex) ;
				}
				byte[] header = DataPackHeader.fullHeader(res_cmd_other ,sendData.length) ;
				dataPack = new byte[sendData.length + header.length] ;
				
				System.arraycopy(header, 0, dataPack, 0, header.length) ;
				System.arraycopy(sendData, 0, dataPack, header.length, sendData.length) ;
				
				String otherSid = user.getSid() ;
				INonBlockingConnection nbc = SessionManager.getINonBlockingConnection(otherSid) ;

				if(nbc != null && nbc.isOpen()){
					System.out.println(" batch 发送 >>>>> "+nbc.getId());
					nbc.write(dataPack) ;
					nbc.flush() ;
				}
				dataIndex ++ ;
			}
		}
	}
	@Override
	public void run() {
		try {
			sendBatch() ;
		} catch (Exception e) {
			e.printStackTrace() ;
		}
		
	}
	
	
	
}
